package com.cls.business.mapper;

import com.cls.business.entity.DeclarePlan;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cls.business.entity.DeclareTopic;
import com.cls.business.vo.EvaluationResultVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 *  Mapper
 *
 * @author slx
 * @date 2020-12-11 10:39:22
 */
@Repository
public interface DeclarePlanMapper extends BaseMapper<DeclarePlan> {

    @Update("update biz_declare_plan set plan_id = null where plan_id = #{planId} and is_delete = false")
    void clearEntrustPlanId(@Param("planId") Long planId);

    @Update("update biz_declare_plan set plan_id = null where declare_plan_id = #{declarePlanId} and is_delete = false")
    void clearPlanIdByDeclarePlanId(@Param("declarePlanId") Long declarePlanId);

    @Select("select a.*, b.topic_id from biz_declare_plan a " +
            "LEFT JOIN biz_declare_topic b on b.declare_id = a.declare_id " +
            "where a.plan_id = #{planId} and a.is_delete = false")
    List<DeclarePlan> selectDeclarePlansByPlanId(@Param("planId") Long planId);

    @Select("select plan.plan_id, topic.name, info.name as leaderName, info.title_type_code,info.company,topic.final_level_type_code,topic.item_code from biz_declare_plan plan " +
            "left join biz_declare_topic topic on topic.declare_id = plan.declare_id " +
            "left join biz_applicant_info info on info.applicant_id = topic.applicant_id " +
            "where  topic.item_code is not null and plan.plan_id = #{planId}")
    List<EvaluationResultVO> findEvaluationResultByPlanId(@Param("planId")Long planId);

    @Select("select * from biz_declare_plan  where plan_id=#{planId} and declare_id=#{declareId} and is_delete = 0 limit 1")
    DeclarePlan findByDeclareTopicIdAndPlanId(@Param("declareId") Long declareId, @Param("planId")Long planId);

    @Select("select topic.* from biz_declare_plan plan " +
            "LEFT JOIN biz_declare_topic topic on topic.declare_id = plan.declare_id " +
            "where plan.plan_topic_id=#{planTopicId} and plan.plan_id=#{planId} and plan.is_delete = 0 ")
    List<DeclareTopic> findDeclareTopicByPlanIdAndTopicId(@Param("planId") Long planId, @Param("planTopicId") Long planTopicId);

    /**
     * 根据申请课题id查询所有的计划
     * @param declareTopicId
     * @return
     */
    @Select("select * from biz_declare_plan  where declare_id=#{declareTopicId}  and is_delete = 0 order by apply_count asc ")
    List<DeclarePlan> findByDeclareTopicIdOrderByApplyCount(@Param("declareTopicId") Long declareTopicId);
}
